//
//  CYCreditDetailTVController.m
//  CreditAddressBook
//
//  Created by Lee on 15/5/11.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYCreditDetailTVController.h"
#import "CYDetailHeadView.h"
#import "CYDetailCell.h"
#import "CYDefaultRecordAO.h"
#import "CYDetailFootView.h"
#import "CYWebDetailCell.h" 
#import "CYABTool.h"
#import "MBProgressHUD+MJ.h"
#import "CYPromiseRecordModel.h"

@interface CYCreditDetailTVController ()<CYWebDetailCellDelegate>

@property (nonatomic, weak) UITableViewCell *cell;
@property (nonatomic, strong) NSNumber *cellH;
@property (nonatomic, strong) NSArray *defrs;//多条违约记录
@end

@implementation CYCreditDetailTVController

- (void)viewDidLoad {
    [super viewDidLoad];
//    if (ios8) {
        CYDetailHeadView *headView = [CYDetailHeadView detailHeadView];
        headView.defr = self.defr;
        headView.backgroundColor = self.view.backgroundColor;
        self.tableView.tableHeaderView = headView;
    
        CYDetailFootView *footView = [CYDetailFootView detailFootView];
        footView.backgroundColor = headView.backgroundColor;
        self.tableView.tableFooterView = footView;
//    }
    self.title = @"违约详情";
    //设置取消按钮
    [self setupCancelBtn];
    //加载网络数据
    [self loadDefrData];
}

/**
 *  通过ID从网络加载数据
 */
- (void)loadDefrData {
        [MBProgressHUD showMessage:@"数据加载中..." toView:self.view];
        NSDictionary *params = @{@"id": self.defr.defaultRecordId, @"type": self.defr.type};
        __weak typeof(self) selfVC = self;
        [CYABTool defaultRecordsbyIdWithParams:params success:^(NSArray *defrs) {
//            [MBProgressHUD hideHUDForView:self.view];
            [MBProgressHUD hideHUDForView:self.view];
            self.defr = [defrs lastObject];
            self.defrs = defrs;
            [self.tableView reloadData];
            
            //加载头部view
            CYDetailHeadView *headView = [CYDetailHeadView detailHeadView];
            headView.defr = self.defr;
            headView.backgroundColor = selfVC.view.backgroundColor;
            self.tableView.tableHeaderView = headView;
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view];
            CYLog(@"根据ID获取违约人详情错误：%@", error);
        }];
}

/**
 *  若pushTag为2或者3,表示该控制器是modal的
 */
- (void)setupCancelBtn {
    if (self.defr.pushTag != 1) {
        UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(cancelClick)];
        self.navigationItem.rightBarButtonItem = bar;
    }
}

- (void)cancelClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.defr.defaultContent && ![self.defr.defaultContent isEqualToString:@""]) {
        CYWebDetailCell *cell = [CYWebDetailCell cellWithTableView:tableView];
        cell.defrs = self.defrs;
        cell.isLoaded = self.cellH.intValue;
        cell.detailDeldgate = self;
        self.cell = cell;
        return cell;
    }
    CYDetailCell *cell = [CYDetailCell cellWithTableView:tableView];
    cell.defrs = self.defrs;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.frame = (CGRect){cell.frame.origin, CGSizeMake(cell.frame.size.width, 150 * self.defrs.count)};
    cell.backgroundColor = self.view.backgroundColor;
    self.tableView.tableFooterView.backgroundColor = self.view.backgroundColor;
    self.cell = cell;
    return cell;
}

- (void)cell:(CYWebDetailCell *)cell cellHeightChange:(double)height {
    self.cellH = [NSNumber numberWithDouble:height];
    [self.tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.defr.defaultContent && ![self.defr.defaultContent isEqualToString:@""]) {
        if (self.cellH) {
            return self.cellH.doubleValue;
        }
        return self.cell.frame.size.height;
    }
    return 150 * self.defrs.count;
}

//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
//    return [CYDetailFootView detailFootView];
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    CYDetailHeadView *headView = [CYDetailHeadView detailHeadView];
//    headView.defr = self.defr;
//    return headView;
//}

- (void)dealloc {
    CYLog(@"黑名单详情页销毁");
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
